package com.example.demo.controller;

import com.alibaba.fastjson.JSON;
import com.example.demo.entity.UserEntity;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.Date;
@Slf4j
@RestController
@RequestMapping("e713")
public class ElasticsearchController713 {

    @Autowired
    RestHighLevelClient restHighLevelClient;


    @PostMapping("add")
    public Object addDocument() throws IOException {

        UserEntity user = UserEntity.builder().age(32).birthDate(new Date())
                .descript("ssss").name("fhz").build();
        // 创建索引请求对象
        IndexRequest indexRequest = new IndexRequest("mydlq-user");
        // 将对象转换为 byte 数组
        byte[] json = JSON.toJSONBytes(user);
        // 设置文档内容
        indexRequest.source(json, XContentType.JSON);
        // 执行增加文档
        IndexResponse response = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
        log.info("创建状态：{}", response.status());

        return "ok";
    }


    @GetMapping("/search")
    public String search() throws IOException {
//        RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("192.168.165.239", 9200, "http")));

        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();

        boolQueryBuilder.must(QueryBuilders.termQuery("loglvl.keyword", "ERROR"));
//        boolQueryBuilder.must(QueryBuilders.wildcardQuery("loglvl.keyword", "INFO"));
//        boolQueryBuilder.must(QueryBuilders.rangeQuery("field").gt("value"));
//        boolQueryBuilder.must(QueryBuilders.termsQuery("loglvl.keyword", "INFO"));

//        boolQueryBuilder.must(QueryBuilders.matchQuery("loglvl", "ERROR"));

        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        sourceBuilder.query(boolQueryBuilder);
        sourceBuilder.sort("@timestamp", SortOrder.DESC);
        sourceBuilder.from(0);
        sourceBuilder.size(10);

        RequestOptions requestOptions = RequestOptions.DEFAULT;

        SearchRequest searchRequest = new SearchRequest("thaipay");
        //searchRequest.searchType(SearchType.QUERY_THEN_FETCH);
        searchRequest.source(sourceBuilder);
        log.info(searchRequest.toString());
        SearchResponse searchResponse = restHighLevelClient.search(searchRequest, requestOptions);
        return JSON.toJSONString(searchResponse);
    }

}
